// Modificación 2.3.7 (EchoTcpClient.cc) - Ejemplo usando el mismo objeto "client" para ambas conexiones

#include "TcpListener.h"

void usage() {
cout << "Usage: EchoTcpClient <name-server-port> <echo-server-domain-name-to-resolve>" << endl;
exit(1);
}

int main(int argc, char** argv) {

if (argc != 3) {
usage();
}

PracticaCaso::TcpClient * client = new PracticaCaso::TcpClient();
client->connect("127.0.0.1", atoi(argv[1]));
string dnsName = argv[2];
client->send(dnsName);
string ipAddressAndPort = client->receive();
client->close();
if (ipAddressAndPort.find("ERROR") == 0) {
cout << "The DNS name " << dnsName << " could not be resolved." << endl;
} else {
cout << "The DNS name: " << dnsName << " corresponds to: " << ipAddressAndPort << endl;
client->connect(ipAddressAndPort.substr(0, ipAddressAndPort.find(":")), atoi((ipAddressAndPort.substr(ipAddressAndPort.find(":")+1)).c_str()));
string msg = "Hello CASO students!";
client->send(msg);
cout << "Message sent: " << msg << endl;
msg = client->receive();
cout << "Message received: " << msg << endl;
client->close();
}
delete client;
}
